package util

import (
	"github.com/360EntSecGroup-Skylar/excelize"
	"mime/multipart"
	"os"
)

func ValidateFileType(fileHeader *multipart.FileHeader, fileTypes ...string) bool {
	contentType, ok := fileHeader.Header["Content-Type"]
	if !ok {
		return false
	}
	for i := 0; i < len(contentType); i++ {
		for _, fileType := range fileTypes {
			if fileType == contentType[i] {
				return true
			}
		}
	}
	return false
}

func TryParseXlsx(file *multipart.File) ([][]string, error) {
	xlsx, err := excelize.OpenReader(*file)
	if err != nil {
		return nil, err
	}
	sheetName := xlsx.GetSheetName(1)
	sheet := xlsx.GetRows(sheetName)
	return sheet, nil
}

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}
